﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using THO7AlgorithmBase;

namespace THO7AlgoritmTimerApplication
{
    class MedianFilter : VisionAlgorithm
    {
        private int _filterSize = 3;
        public int filterSize
        {
            get
            {
                return _filterSize;
            }
            set
            {
                _filterSize = value;
            }
        }
        
        public MedianFilter(String name) : base(name) { }
        public override System.Drawing.Bitmap DoAlgorithm(System.Drawing.Bitmap sourceImage)
        {
            Bitmap returnImage = new Bitmap(sourceImage);
            int[] rgbValues = MyImage.BmpToArray(returnImage);

            //Modified pixelArray            
            int[] rgbValues2 = rgbValues;
        
            int counter = 0;
            int width = sourceImage.Width;
            int height = sourceImage.Height; 

            //Arrays for each of the color channels
            int[] blueArray = new int[_filterSize * _filterSize];
            int[] greenArray = new int[_filterSize * _filterSize];
            int[] redArray = new int[_filterSize * _filterSize];

            for (int i = ((width) + 1) * (_filterSize / 2); i < ((width) * height) - (width + 1) * (_filterSize / 2); i++)
                {
                    if (i % (width) < (_filterSize / 2) || i % (width) == (width) - (_filterSize / 2))
                {
                     //Skip the first and last pixels depending on the filter size
                     continue;
                }
                //Loops through the filter
                for (int y = (-(_filterSize /2)); y <= (_filterSize /2); y++)
                {
                    for (int x = (-(_filterSize / 2)); x <= (_filterSize / 2); x++)
                    {
                        //Fils the arrays
                        blueArray[counter] = rgbValues[i + x + (y * (width))] & 255;
                        greenArray[counter] = (rgbValues[i + x + (y * (width))] >> 8) & 255;
                        redArray[counter] = (rgbValues[i + x + (y * (width))] >> 16) & 255;
                        counter++;
                    }
                }
                //Sorts the arrays
                Array.Sort(blueArray);
                Array.Sort(greenArray);
                Array.Sort(redArray);

                //Creates the color for the int
                rgbValues2[i] = 255 << 24 | (Convert.ToByte(redArray[4]) << 16) |(Convert.ToByte(greenArray[4]) << 8) | Convert.ToByte(blueArray[4]);
                counter = 0;
            }

            //Returns the modified image
            returnImage = MyImage.ArrayToBmp(rgbValues2, returnImage);
            Bitmap orignalImage = new Bitmap(sourceImage);
            //FilterTest.MedianTest(orignalImage, returnImage, 3);
            return returnImage;
        }
    }
}






